/*
 * @Author: your name
 * @Date: 2021-04-26 17:49:37
 * @LastEditTime: 2021-06-01 17:18:26
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \wa-manage-view\src\utils\decorator.js
 */
// 此处为对应的装饰器辅助类
import {
	reqDataField
} from './parameter';
// 注册当前请求对应的上下文请求组
// import {
// 	request
// } from 'bus-common-component/lib/extension';
class request{
  setOption(obj){
    console.log(obj,'请求头')
  }
}
// 引入成功或失败相关方法
import {
	InterceptResponse,
	handleError
} from '@/api/error';
// 获取指定上下文地址的查询路由
const services = new request({
	address: "/wa-manage-server",
	InterceptResponse,
	handleError
});

/**
 * 请求方法装饰器
 * @description: 用于将请求的参数进行对应的默认格式转换处理,防止抛空等问题
 * @param {*} data 当前请求传入参数
 * @return {*}
 */
export function Parameters(data = []) {
	return function (target, name, descriptor) {
		let func = descriptor.value;
		descriptor.value = async function (...args) {
			let obj = args[0] || {};
			// 声明公共参数接收
			this["param"] = {};
			// 传入指定的地址请求
			this.services = services;
			obj = reqDataField(obj, false);
			data.map(item => {
				if (obj[item] != undefined) {
					// this[item] = obj[item];
					this.param[item] = obj[item];
				}
				if(item == 'FormData'){
					this.param = obj;
					return;
				}
			})
			let res = await func.apply(this);
			// 请求成执行
			if (res.code == 0 && res.data) {
				res.data = reqDataField(res.data, true);
			}
			return res;
		}
	}
}
